﻿/*
 * @file: RolesDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class RolesDA : BaseDAL, IRoles
    {
        private const string tableName = "Roles";
        private const string roleID = "RoleID";
        private const string roleName = "RoleName";
        private const string description = "Description";
        private const string isEnable = "IsEnable";

        public RolesDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IRoles Members

        /*
         * @method: GetRoles
         * @description: get all roles
         * @input: allowFilter <TRUE enables ignore hidden records>
         * @output: record corresponding
         */
        public Roles[] GetRoles(bool allowFilter)
        {
            Roles[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = 1",
                        tableName, isEnable);
                else
                    cmd.CommandText = String.Format("SELECT * FROM {0}", tableName);

                String[] columnNames = {    roleID,
                                            roleName,
                                            description,
                                            isEnable};
                result = SelectCollection<Roles>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static int RoleNameExisted(string _roleName)
        {
            try
            {
                return RecordExisted(tableName, roleName, roleName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /*
         * @method: GetRoleByRoleID
         * @description: get a Roles by RoleID
         * @input: RoleID, allowFilter <TRUE enables ignore hidden records>
         * @output: record corresponding
         */
        public Roles GetRoleByRoleID(int _roleID, bool allowFilter)
        {
            Roles[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, roleID, roleID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", roleID), SqlDbType.Int).Value = _roleID;
                }
                else
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, roleID, roleID);
                    cmd.Parameters.Add(String.Format("@{0}", roleID), SqlDbType.Int).Value = _roleID;
                }

                String[] columnNames = {    roleID,
                                            roleName,
                                            description,
                                            isEnable};
                result = SelectCollection<Roles>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        /*
         * @method: InsertRole
         * @description: insert new Role to database
         * @input: Role entity
         * @output: 1 if insert successful, 0 if error occurred
         */
        public int InsertRole(Roles _role, out int autoNumber)
        {
            int result = 0;
            try
            {
                
                String[] columnNames = {    roleName, 
                                            description};

                Object[] values = { _role.RoleName,
                                    _role.Description};

                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: UpdateRole
         * @description: update Role information
         * @input: Roles entity
         * @output: 1 if successful, 0 if error occurred
         */
        public int UpdateRole(Roles _role)
        {
            int result = 0;
            try
            {

                String[] columnNames = {    roleName,
                                            description};
                Object[] values = { _role.RoleName,
                                    _role.Description};
                String[] keyColumnNames = { roleID };
                Object[] keyColumnValues = { _role.RoleID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: DisableRole
         * @description: hidden a ShippingDetails
         * @input: RoleID, isEnable <FALSE if allow hidden>
         * @output: 1 if update successful, 0 if error occurred
         */
        public int DisableRole(int _roleID, bool _isEnable)
        {
            int result = 0;
            try
            {
                String[] columnNames = { isEnable };
                Object[] values = { _isEnable };
                String[] keyColumnNames = { roleID };
                Object[] keyColumnValues = { _roleID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}
